﻿namespace Minesweeper
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    public class MineField
    {
        private const char block = (char)0x2588;

        private readonly int height;
        private readonly int width;
        private readonly int numberOfMines;

        private int currentRow;
        private int currentCol;
        private Cell[,] field;

        public MineField(int height, int width, int numberOfMines)
        {
            this.height = height;
            this.width = width;
            this.numberOfMines = numberOfMines;
            this.currentRow = 0;
            this.currentCol = 0;
            InitializeField();
        }

        #region Properties
        public int Width
        {
            get
            {
                return this.width;
            }
        }

        public int Height
        {
            get
            {
                return this.height;
            }
        }

        public int NumberOfMines
        {
            get
            {
                return this.numberOfMines;
            }
        }

        public int CurrentRow
        {
            get
            {
                return this.currentRow;
            }
            set
            {
                this.currentRow = value;
            }
        }

        public int CurrentCol
        {
            get
            {
                return this.currentCol;
            }
            set
            {
                this.currentCol = value;
            }
        }

        public Cell CurrentCell
        {
            get
            {
                return this[currentRow, currentCol];
            }
        }

        public Cell this[int row, int col]
        {
            get
            {
                return this.field[row, col];
            }
            set
            {
                this.field[row, col] = value;
            }
        }
        #endregion

        public void Visit(int row, int col)
        {
            this[row, col].Visit();
        }

        public bool HasMineAt(int row, int col)
        {
            return this.field[row, col].IsMine;
        }

        public void InitialDraw()
        {
            Console.WriteLine();
            for (int row = 1; row < this.Height * 5 + 1; row++)
            {
                Console.Write(" ");
                for (int col = 0; col < this.Width; col++)
                {
                    Console.Write("{0}{0}{0} ", block);
                }
                Console.WriteLine();
                if (row % 5 == 0)
                {
                    Console.WriteLine();
                }
            }
        }

        private void InitializeField()
        {
            this.field = new Cell[this.height, this.width];
            int currentX = 0;
            int currentY = 0;
            for (int row = 0; row < this.field.GetLength(0); row++)
            {
                for (int col = 0; col < this.field.GetLength(1); col++)
                {
                    this.field[row, col] = new Cell(currentX, currentY, false);
                    currentX += Cell.CellWidth + 1;
                }

                currentY += Cell.CellHeight + 1;
                currentX = 0;
            }
        }
    }
}
